'use strict';
/**
 * Created by FreezeSoul on 2015/10/10.
 */
define([
        'angular',
        'adConfig',
        'app/modules/page/define'
    ],
    function (angular, adConfig) {
        angular.module('ad.page')
            .factory('adPageEditService', ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getPage(current, size, search) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/search';
                    adRequest(url, {
                        criteria: search, pageIndex: current, pageSize: size
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function getPages() {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/get';
                    adRequest(url, {}).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function addPage(item) {
                    var pageName = item.page_name;
                    var folderId = item.page_folder_id;
                    var pageHome = item.page_home;
                    var pageTypeId = item.page_type_id;
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/add';
                    adRequest(url, {
                        pageName: pageName,
                        folderId: folderId,
                        pageHome: pageHome,
                        pageTypeId: pageTypeId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function removePages(items) {
                    var pageIds = _.map(items, function (item) {
                        return item.page_id;
                    });
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/delete';
                    adRequest(url, {
                        pageIds: angular.toJson(pageIds)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadPage(pageId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/load';
                    adRequest(url, {
                        pageId: pageId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function refreshPage(pageId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/refresh';
                    adRequest(url, {
                        pageId: pageId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function updatePage(item) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'chart/page/update';
                    adRequest(url, {
                        pageId: item.page_id,
                        pageName: item.page_name,
                        pageHome: item.page_home,
                        pageData: item.page_data,
                        folderId: item.page_folder_id,
                        pageTypeId: item.page_type_id,
                        pageVersion: item.page_version++
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function exportPage(pageId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + '/common/resource/page/export';
                    adRequest(url, {
                        pageId: pageId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                return {
                    getPage: getPage,
                    getPages: getPages,
                    removePages: removePages,
                    addPage: addPage,
                    loadPage: loadPage,
                    updatePage: updatePage,
                    refreshPage: refreshPage,
                    exportPage: exportPage
                };
            }]);
    });